<template>
  <view class="register-container">
    <view class="form-box">
      <text class="title">注册</text>
      <view class="input-item">
        <input type="text" placeholder="请输入用户名" v-model="username" />
      </view>
      <view class="input-item">
        <input type="password" placeholder="请输入密码" v-model="password" />
      </view>
      <view class="input-item">
        <input type="password" placeholder="请确认密码" v-model="confirmPwd" />
      </view>
      <button class="register-btn" @click="handleRegister">注册</button>
      <navigator url="/pages/login/login" class="nav-link">已有账号？去登录</navigator>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      confirmPwd: ''
    }
  },
  methods: {
    handleRegister() {
      if (!this.username || !this.password || !this.confirmPwd) {
        uni.showToast({
          title: '请填写完整信息',
          icon: 'none'
        })
        return
      }
	  uniCloud.callFunction({
	  	name:'addUser',
	  		data:{
	  			'name':this.username,
	  			'password':this.password
	  		},
	  		success:(msg) => {
	  			console.log("addUser callback "+JSON.stringify(msg));
	  			setTimeout(()=>{
	  				uni.hideLoading()
	  				uni.showToast({
	  					title:'注册成功'
	  				})
	  				uni.navigateBack();
	  			},1000)
	  		},
	  		fail:(errer) => {
	  			console.log("addUser fail" + errer);
	  		}
	  })
      if (this.password !== this.confirmPwd) {
        uni.showToast({
          title: '两次密码不一致',
          icon: 'none'
        })
        return
      }
      uni.showToast({
        title: '注册成功',
        icon: 'success'
      })
      // 注册成功后跳转到登录页
      setTimeout(() => {
        uni.navigateTo({
          url: '/pages/login/login'
        })
      }, 1500)

    }
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f8f8f8;
}
.form-box {
  width: 80%;
  background-color: #fff;
  padding: 30rpx;
  border-radius: 12rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
}
.title {
  display: block;
  text-align: center;
  font-size: 40rpx;
  font-weight: bold;
  margin-bottom: 40rpx;
}
.input-item {
  margin-bottom: 30rpx;
}
.input-item input {
  width: 100%;
  height: 80rpx;
  border: 1rpx solid #ddd;
  border-radius: 6rpx;
  padding: 0 20rpx;
  box-sizing: border-box;
}
.register-btn {
  width: 100%;
  height: 80rpx;
  background-color: #07c160;
  color: #fff;
  border-radius: 6rpx;
  margin-bottom: 20rpx;
}
.nav-link {
  display: block;
  text-align: center;
  font-size: 28rpx;
  color: #007aff;
}
</style>
